package com.qb.mobile;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.qb.mobile.R;
import com.qb.mobile.adapter.PhonebookAdapter;
import com.qb.mobile.core.actionbar.Action;
import com.qb.mobile.core.actionbar.ActionBar;
import com.qb.mobile.core.actionbar.IntentAction;
import com.qb.mobile.manager.ContactsReader;
import com.qb.mobile.model.Phonebook;

/**
 * 
 * @author <a href="mailto:dangcongthanhtrung@gmail.com">dangtrung</a>
 * 
 *         PhonebookList.java class
 * 
 */
public class PhonebookList extends Activity {

	private List<Phonebook> listOfPhonebook;
	private PhonebookAdapter adapter;
	private ContactsReader contactsReader;
	private ActionBar actionBar;
	private Intent intent;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_book_list);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		contactsReader = new ContactsReader(this);
		initListView();
		initAction();
	}

	public void onResume() {
		super.onResume();
		
		actionBar.setTitle(getResources().getString(R.string.title_home));
		TextView txtEmpty = (TextView) findViewById(R.id.sys_txt_all_contact_import);
		if (listOfPhonebook.size() > 0)
			txtEmpty.setVisibility(View.INVISIBLE);
		else
			txtEmpty.setVisibility(View.VISIBLE);
		
		
		String ids = getListOfPhonebookIdSepratedByCommas();
		intent.putExtra("Ids", ids);
	}

	private void initAction() {
		actionBar = (ActionBar) findViewById(R.id.actionbar);
		Action importAction = new IntentAction(this, createImportIntent(),
				R.drawable.ic_import_default, 1);
		actionBar.addAction(importAction);
	}

	private void initListView() {
		ListView listView = (ListView) findViewById(R.id.phone_book_list_list_view);
		listOfPhonebook = new ArrayList<Phonebook>();
		contactsReader.createOrConnectDatabase();
		contactsReader.getPhonebookFromDatabase(listOfPhonebook);
		adapter = new PhonebookAdapter(this, listOfPhonebook);
		listView.setAdapter(adapter);
	}

	public Intent createImportIntent() {
		intent = new Intent(this, SystemPhonebookList.class);
		String ids = getListOfPhonebookIdSepratedByCommas();
		intent.putExtra("Ids", ids);
		return intent;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				Phonebook phoneBook = (Phonebook)data.getSerializableExtra("SystemBook");
				if (!checkContactExisted(phoneBook.getId())) {
					contactsReader.insertPhonebookIntoDatabase(phoneBook);
					contactsReader.getPhonebookFromDatabase(listOfPhonebook);
					adapter.notifyDataSetChanged();
				}
			}
		} else {
			if (resultCode == RESULT_OK) {
				String id = data.getStringExtra("Id");
				contactsReader.deletePhonebookFromDatabase(id);
				contactsReader.getPhonebookFromDatabase(listOfPhonebook);
				adapter.notifyDataSetChanged();
			}
		}
	}

	private boolean checkContactExisted(String id) {
		for (int i = 0; i < listOfPhonebook.size(); i++) {
			Phonebook entry = listOfPhonebook.get(i);
			if (entry.getId().equalsIgnoreCase(id))
				return true;
		}
		return false;
	}

	private String getListOfPhonebookIdSepratedByCommas() {
		String result = "";
		for (int i = 0; i < listOfPhonebook.size(); i++) {
			Phonebook phonebook = listOfPhonebook.get(i);
			if (i == 0)
				result = result + phonebook.getId();
			else
				result = result + "," + phonebook.getId();
		}
		return result;
	}
}